package com.zcl.tool.utils;

import com.itextpdf.text.*;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.lowagie.text.Cell;
import com.lowagie.text.Table;
import com.lowagie.text.rtf.RtfWriter2;

import javax.servlet.ServletOutputStream;
import java.awt.*;
import java.io.*;
import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: zhanchenlei
 * @Date: 2021/06/02/9:28
 * @Description:
 */
public class PdfUtils {
    public static String downloadPdf()  throws Exception {
// List<Notice> list = noticeService.searchNotice(map);搜寻信息

        File file = null;
        InputStream fin = null;
        ServletOutputStream out = null;
        //生成pdf
        Document document = new Document(PageSize.A4, 25, 25, 25, 25);

        //创建文件夹
        String filePar = null;
        String file1="D:";

        filePar = file1+new Date().getTime();// 文件夹路径
        File myPath = new File( filePar );
        file = new File(filePar+"/消息列表"+ new Date().getTime()+".doc");
        if (!myPath.exists()){
            myPath.mkdir();
        }
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        document.open();
        //设置中文字体
        BaseFont bfChinese = null;// FontFactory.getFont(FontFactory.COURIER,
        // 14, Font.BOLD, new CMYKColor(0, 255, 0, 0);//大小，粗细，颜色
        try {
            bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
                    BaseFont.NOT_EMBEDDED);
        } catch (IOException | DocumentException e) {
            e.printStackTrace();
        }
        //设置字体
        Font f10 = new Font(bfChinese, 10, Font.NORMAL);
        Font f12 = new Font(bfChinese, 12, Font.NORMAL);
        Font f26 = new Font(bfChinese, 26, Font.NORMAL);//一号字体
        //创建标题
        Paragraph title1 = new Paragraph("测试", f26);
        title1.setAlignment(Element.ALIGN_CENTER);
        Chapter chapter1 = new Chapter(title1, 1);
        chapter1.setNumberDepth(0);
        Section section1 = chapter1;

		/*Paragraph title2 = new Paragraph("老板，请问有没有哇哈哈~~矿泉水。", f12);
		title2.setLeading(22f);//-行间距
		section1.add(title2);*/

        // 创建4列的表格
        int colNumber = 3;
        PdfPTable t = new PdfPTable(colNumber);
        //设置段落上下空白
        t.setSpacingBefore(25);
        t.setSpacingAfter(25);
        t.setHorizontalAlignment(Element.ALIGN_CENTER);// 居左
        float[] cellsWidth = { 0.2f, 0.55f, 0.25f }; // 定义表格的宽度
        t.setWidths(cellsWidth);// 单元格宽度
        t.setTotalWidth(500f);//表格的总宽度
        t.setWidthPercentage(100);// 表格的宽度百分比
        //设置表头
        PdfPCell c1 = new PdfPCell(new Paragraph("ID",f12));
        t.addCell(c1);
        PdfPCell c2 = new PdfPCell(new Paragraph("消息内容",f12));
        t.addCell(c2);
        PdfPCell c3 = new PdfPCell(new Paragraph("创建时间",f12));
        t.addCell(c3);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
        //遍历数据
        /*for (Notice notice : list) {
            t.addCell(new Paragraph(String.valueOf(notice.getId()),f10));
            t.addCell(new Paragraph(notice.getContent(),f10));
            t.addCell(new Paragraph(sdf.format(notice.getCreate_time()),f10));
        }*/
        t.addCell(new Paragraph("123",f10));
        t.addCell(new Paragraph("fd",f10));
        t.addCell(new Paragraph("bend",f10));
        t.addCell(new Paragraph("123",f10));
        t.addCell(new Paragraph("fd",f10));
        t.addCell(new Paragraph("bend",f10));
        section1.add(t);
        document.add(chapter1);
        document.close();
        /*******pdf文件生成结束*********/

        /*try {
            fin = new FileInputStream(file);
            resp.setCharacterEncoding("utf-8");
            resp.setContentType("application/pdf");
            String filename = "消息列表";
            // 设置浏览器以下载的方式处理该文件
            if (req.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) {
                filename = URLEncoder.encode(filename, "UTF-8");
            } else {
                filename = new String(filename.getBytes("UTF-8"), "ISO8859-1");
            }
            resp.setHeader("Content-Disposition", "attachment;filename=" + filename + ".pdf");//下载使用
            out = resp.getOutputStream();
            byte[] buffer = new byte[512]; // 缓冲区
            int bytesToRead = -1;
            // 通过循环将读入的Word文件的内容输出到浏览器中
            while ((bytesToRead = fin.read(buffer)) != -1) {
                out.write(buffer, 0, bytesToRead);
            }
        } finally {
            if (fin != null)
                fin.close();
            if (out != null)
                out.close();
            if (file != null)
                file.delete(); // 删除临时文件
            if (myPath != null)// 删除临时文件夹
                myPath.delete();
        }*/
        return null;
    }



    public static String exportDoc()throws Exception {
        File file = null;
        try {
            // 设置纸张大小
            com.lowagie.text.Document document = new com.lowagie.text.Document(com.lowagie.text.PageSize.A4);
            // 建立一个书写器(Writer)与document对象关联，通过书写器(Writer)可以将文档写入到磁盘中
            // ByteArrayOutputStream baos = new ByteArrayOutputStream();
            // C:\\Users\\orion\\Desktop\\home.jpg
            //创建文件夹
            String filePar = null;
            String file1="D:";

            filePar = file1+new Date().getTime();// 文件夹路径
            File myPath = new File( filePar );
            file = new File(filePar+"/消息列表"+ new Date().getTime()+".doc");
            if (!myPath.exists()){
                myPath.mkdir();
            }
           // File file = new File("D://2003-1.doc");
            RtfWriter2.getInstance(document, new FileOutputStream(file));
            document.open();
            // 设置中文字体
            com.lowagie.text.pdf.BaseFont bfChinese = com.lowagie.text.pdf.BaseFont.createFont(com.lowagie.text.pdf.BaseFont.HELVETICA, com.lowagie.text.pdf.BaseFont.WINANSI, com.lowagie.text.pdf.BaseFont.NOT_EMBEDDED);
            // 标题字体风格
           // Font titleFont = new Font(bfChinese, 12, Font.BOLD);
            /*Paragraph title = new Paragraph("测试结果");
            // 设置标题格式对齐方式
            title.setAlignment(Element.ALIGN_CENTER);
            title.setFont(titleFont);
            document.add((com.lowagie.text.Element) title);*/
            // 正文字体风格
           // com.lowagie.text.Font contextFont = new com.lowagie.text.Font(bfChinese, 12, com.lowagie.text.Font.BOLD);
             //Font contextFont = new Font(bfChinese, 11, Font.NORMAL);
            //创建标题
            com.lowagie.text.Paragraph paragraph = new com.lowagie.text.Paragraph("测试");
            paragraph.setAlignment(Element.ALIGN_CENTER);
            document.add(paragraph);

            /*
             * 创建有三列的表格
             */
            Table table = new Table(5);//设置表格列数
            table.setBorderWidth(1);
            table.setBorderColor(Color.BLACK);
            table.setPadding(0);
            table.setSpacing(0);

            /*
             * 添加表头的元素
             */
            Cell cell = new Cell();//单元格
            cell.setColspan(5);//设置表格为三列
            cell.setRowspan(2);//设置表格为三行

            // 表格的主体
            cell = new Cell();
            cell.setRowspan(2);//当前单元格占两行,纵向跨度
            //table.addCell(cell);
            table.addCell("1,1");
            table.addCell("1,2");
            table.addCell("1,3");
            table.addCell("1,4");
            table.addCell("1,5");
            table.addCell(new com.lowagie.text.Paragraph("用java生成的表格1"));
            table.addCell(new com.lowagie.text.Paragraph("用java生成的表格2"));
            table.addCell(new com.lowagie.text.Paragraph("用java生成的表格3"));
            table.addCell(new com.lowagie.text.Paragraph("用java生成的表格4"));
            table.addCell(new com.lowagie.text.Paragraph("用java生成word文件"));
            document.add(table);
            document.close();
            // 得到输入流
            // wordFile = new ByteArrayInputStream(baos.toByteArray());
            // baos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (com.lowagie.text.BadElementException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (com.lowagie.text.DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";

    }

    public static void main(String[] args) throws Exception {

    }
}
